<%@page import="org.qloudgen.rul.drools.rule.banking.Banking"%>
<%@page import="org.qloudgen.sec.cas.client.WebUtil"%>
<%@page import="org.qloudgen.sec.shiro.SecurityUtil"%>
<%@page import="org.apache.shiro.subject.Subject"%>
<%@page import="org.qloudgen.sec.shiro.permission.DroolsPermission"%>
<%@page import="java.util.Map"%>


<jsp:directive.include file="top.jsp" />

<%
    Banking tran = (request.getAttribute("tran") == null ? new Banking():(Banking)request.getAttribute("tran"));
    
    String sacno = request.getParameter("sacno") == null ? null:(String)request.getParameter("sacno");
    tran.setAcno(sacno);
    
    String actype = request.getParameter("actype") == null ? null:(String)request.getParameter("actype");
    tran.setActype(actype);
    
    
    
    String amount = request.getParameter("amount") == null ? "0" : (String)request.getParameter("amount");
    amount = amount.replaceAll(",","");
    tran.setAmount(new Double(amount));
    
    String balance = request.getParameter("accountb") == null ? "100000.00" : (String)request.getParameter("accountb");
    balance = balance.replaceAll(",","");
    tran.setBalance(new Double(balance));
    
    //String userId = WebUtil.getUserId();
    //Map<String,Object> userMap = WebUtil.getUserAttributes();
    
    
    tran.setClientName("qongqiqi");
    //tran.setClientName((String)userMap.get("clientName"));
    String clientType = request.getParameter("clientType") == null ? "" : (String)request.getParameter("clientType");
    tran.setClientType(clientType);
    tran.setPhone("13900000000");
    //tran.setPhone((String)userMap.get("clientPhone"));
    
    request.setAttribute("tran",tran);
    
    
    boolean test = authentication(tran);

%>
<%!
    public boolean authentication(Banking tran){
       
       //boolean result = WebUtil.isPermitted( "AllowedTran" , new Object[]{ tran }  );
       Subject subject = SecurityUtils.getSubject();
       boolean result = subject.isPermitted( new DroolsPermission( "AllowedTran" , new Object[]{ tran } ) );
       return result;
    } 
    
    
%>
<script type="text/javascript">
	function www_zzjs_net (BankNo)
	{
		if (BankNo.value == "") return;
		var account = new String (BankNo.value);
		account = account.substring(0,22); 
		if (account.match (".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null)
		{
		
			if (account.match (".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" + ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" +
			".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" + ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null)
			{
				var accountNumeric = accountChar = "", i;
				for (i=0;i<account.length;i++)
				{
					accountChar = account.substr (i,1);
					if (!isNaN (accountChar) && (accountChar != " ")) accountNumeric = accountNumeric + accountChar;
				}
				account = "";
				for (i=0;i<accountNumeric.length;i++)
				{ 
				   if (i == 4) account = account + " "; 
				   if (i == 8) account = account + " "; 
				   if (i == 12) account = account + " ";
				   account = account + accountNumeric.substr (i,1)
				}
			}
		}
		else
		{
		    account = " " + account.substring (1,5) + " " + account.substring (6,10) + " " + account.substring (14,18) + " " + account.substring(18,25);
		}
		if (account != BankNo.value) BankNo.value = account;
	}
	function checkBankNo (BankNo)
	{
		if (BankNo.value == "") return;
		if (BankNo.value.match (".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null)
		{
		if (BankNo.value.match ("[0-9]{19}") != null)
		www_zzjs_net (BankNo)
		}
	}
    function checkEnterForFindListing(e){
		var characterCode;
		if(e && e.which){
			e = e;
			characterCode = e.which ;
		}
		else{
			e = event;
			characterCode = e.keyCode;
		}
		if(characterCode == 22){
			document.forms[getNetuiTagName("findListingForm")].submit();
			return false;
		}
		else{
		    return true ;
		}
	}
	
   function formatCurrency(num) {
	    num = num.toString().replace(/\$|\,/g,'');
	    if(isNaN(num))
	        num = "0";
	    sign = (num == (num = Math.abs(num)));
	    num = Math.floor(num*100+0.50000000001);
	    cents = num%100;
	    num = Math.floor(num/100).toString();
	    if(cents<10)
	    cents = "0" + cents;
	    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	    num = num.substring(0,num.length-(4*i+3))+','+
	    num.substring(num.length-(4*i+3));
	    return (((sign)?'':'-') + num + '.' + cents);
	}
	
	function putAmount(){
		
		var num = document.getElementById("amount").value;
		num = formatCurrency(num);
		document.getElementById("amount").value = num;
		
	}
	
   function transfer(){
   	   
   	   /*document.getElementById("clientTypebackup").value = '';
   	   document.getElementById("tacnobackup").value = '';
   	   document.getElementById("actypebackup").value = '';
   	   document.getElementById("trantypebackup").value = '';
   	   document.getElementById("amountbackup").value = '';
   	   document.getElementById("accountbbackup").value = '';
   	   
   	   var amount = document.getElementById("amount").value;
   	   amount = amount.replace(new RegExp(",","gm"),"");
   	   //alert("amount="+amount);
   	   var balance = document.getElementById("accountb").value;
   	   balance = balance.replace(new RegExp(",","gm"),"");
   	   
   	   var clientType = document.getElementById("clientType").value;
   	   
   	   if ( parseFloat(amount) > parseFloat(balance) || (parseFloat(amount) > 5000 && clientType != "VIP")){
   	      document.getElementById("clientTypebackup").value = document.getElementById("clientType").value;
   	      document.getElementById("tacnobackup").value = document.getElementById("tacno").value;
   	      document.getElementById("actypebackup").value = document.getElementById("actype").value;
   	      document.getElementById("trantypebackup").value = document.getElementById("trantype").value;
   	      document.getElementById("amountbackup").value = document.getElementById("amount").value;
   	      document.getElementById("accountbbackup").value = document.getElementById("accountb").value;
   	   }*/
   }
</script>
<h1>Account Transfer</h1>
<%
	    if (test == false){
	      out.println("<div id='msg' class='errors'>");
          out.println("Transfer Error:" + tran.getErrMessage());
          out.println("</div>");
          tran.setBalance(new Double(balance));
        }
        
        if (test == true && request.getQueryString() != null){
	      out.println("<div id='msg' class='success'>");
          out.println("Transfer Success.");
          out.println("</div>");
          //tran.setBalance(new Double(newBalance));
        }
%>
<div id="transaction">
	
	<form action="transfer.jsp" id="fmtran" >
		<table onload="test()">
			<fieldset style="border:1;">
			    <legend>Client Info</legend>
			    <span class="myField">
				    ClientName :<input type="text" readOnly="true" id="clientName" name="clientName" value="<%= userMap.get("clientName")%>" style="background-color:#eee"/>
				    <label class="postField" ></label>
				    ClientType:<input type="text" id="clientType" name="clientType" list="client_type_list" required="required"/>
                    <datalist id="client_type_list">
							<option label="VIP Client, Can Transfer Amount > 5000.00" value="VIP" />
							<option label="Normal Client, Can not Transfer Amount > 5000.00" value="Normal" />
					</datalist>
					<input type="hidden" id="clientTypebackup" name="clientTypebackup" value="<%=request.getParameter("clientTypebackup") == null ? "" : request.getParameter("clientTypebackup")%>" />
				    <label class="postField" ></label>
					ClientPhone:<input type="text" readOnly="true" id="clientPhone" name="clientPhone" value="<%= userMap.get("clientPhone")%>" style="background-color:#eee"/>
	     		</span>
			</fieldset>
		</table>
		<br />
		<table>
			<fieldset style="border:1;">
				 <legend>Transaction Info</legend>
				 <span class="oneField">
					    <label for="sacno" class="preField">Outgoing Account:</label>
					    <input id="sacno" type="text" size="20" required="required" name="sacno" autocomplete="off" readOnly="true" value="8888 8888 8888 8888" style="background-color:#eee"/>
					    <br />
				 </span>
				 <span class="oneField">
						<label for="tacno" class="preField">Incoming Account:</label>
						<input id="tacno" type="text" size="20" maxlength="19" required="required" name="tacno" autocomplete="off" onkeyup="www_zzjs_net(this)" onkeydown="www_zzjs_net(this)"/>
						<input id="tacnobackup" type="hidden" name="tacnobackup"   value="<%=request.getParameter("tacnobackup") == null ? "" : request.getParameter("tacnobackup")%>" />
					<br />
				 </span>
				 <span class="oneField">		
						<label for="actype" class="preField">Account Type:</label>
						<input type="text" id="actype" size="20" list="account_type_list" required="required" name="actype" autocomplete="off"/>			  
		                <datalist id="account_type_list">
							<option label="Current Account" value="CA" />
							<option label="Deposit Account" value="DA" />
					    </datalist>		
					    <input type="hidden" id="actypebackup" name="actypebackup" value="<%=request.getParameter("actypebackup") == null ? "" : request.getParameter("actypebackup")%>" />			  
		                <br />
				  </span>
				  <!--span class="oneField">
					    <label for="name" class="preField">Transaction Type:</label>
					    <input type="text" id="trantype" size="20" list="transaction_type_list" required="required" name="trantype" autocomplete="off"/>
	                    <datalist id="transaction_type_list">
							<option label="Self Bank Transfer" value="SBT" />
							<option label="Other Bank Transfer" value="OBT" />
						</datalist>
						<input type="hidden" id="trantypebackup" name="trantypebackup" value="<%=request.getParameter("trantypebackup") == null ? "" : request.getParameter("trantypebackup")%>" />
						<br />
				  </span-->
				  <span class="oneField">
						<label for="name" class="preField">Transfer Amount:</label>
						<input type="text" id="amount" name="amount" required="required" name="amount" autocomplete="off" onblur="putAmount()"/>
						<input type="hidden" id="amountbackup" name="amountbackup" value="<%=request.getParameter("amountbackup") == null ? "" : request.getParameter("amountbackup")%>" />
				  </span>
			</fieldset>
		</table>
		<br />
		
		<table>
			<fieldset style="border:1;">
			    <legend>Account Info</legend>
			    <span class="myField">
				     Account Balance:<input type="text" readOnly="true" id="accountb" name="accountb" value="<%=tran.getBalance()%>" style="background-color:#eee"/>
				     <label class="postField" ></label>
				     <input type="hidden" id="accountbbackup" name="accountbbackup" value="<%=request.getParameter("accountbbackup") == null ? "" : request.getParameter("accountbbackup")%>" />
				     <!--Available Balance:<input type="text" readOnly="true" id="availableb" name="availableb" value="10000.00" style="background-color:#eee"/-->
			    </span>
			</fieldset>
						
		</table>
		<br />
		<table>
			<tr>
				<td><input type="submit" class="primaryAction" value="Transfer" onclick="transfer()"/></td>	
			</tr>
		</table>
	</form>
</div>
<script type="text/javascript">
   
   var formatBalance = formatCurrency(document.getElementById("accountb").value);
   document.getElementById("accountb").value = formatBalance;
   
   <%
    if (test == false){
    %>
   document.getElementById("clientType").value = "<%= request.getParameter("clientType") == null ? "" : (String)request.getParameter("clientType") %>";
   document.getElementById("tacno").value = "<%= request.getParameter("tacno") == null ? "" : (String)request.getParameter("tacno") %>";//document.getElementById("tacnobackup").value;
   document.getElementById("actype").value = "<%= request.getParameter("actype") == null ? "" : (String)request.getParameter("actype") %>";
   //document.getElementById("trantype").value = "<%= request.getParameter("trantype") == null ? "" : (String)request.getParameter("trantype") %>";
   document.getElementById("amount").value = "<%= request.getParameter("amount") == null ? "" : (String)request.getParameter("amount") %>";
   document.getElementById("amount").focus();
   <%
    }
   %> 

</script>
<jsp:directive.include file="bottom.jsp" />